<template>
  <view style="background-color: #efefef;">
    <!-- 轮播图区域 -->
    <swiper class="goods-swiper" :autoplay="true" :circular="true" :indicator-dots="true" :duration="1000">
      <swiper-item class="goods-swiper-item" v-for="(item,i) in goodsInfo.pics" :key="i" @click="previewImg(i)">       
          <image :src="item.pics_big"></image>        
      </swiper-item>
    </swiper>
    <!-- 商品介绍区域 -->
    <view class="goods-introduce">
      <view class="price" v-if="goodsInfo.goods_price">￥{{goodsInfo.goods_price}}</view>
      <view class="introduce">{{goodsInfo.goods_name}}</view>
    </view>
    <!-- 商品详情 -->
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
    <!-- 商品导航 -->
    <view class="goods-nav">
      <uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
    </view>
    
  </view>
</template>

<script>
import { getShopDetail } from '../../api/request';
import { mapState,mapMutations,mapGetters} from 'vuex'
import badge from '../../mixins/tabbar-badge.js'
  export default {
    mixins:[badge],
    onLoad(options) {
      const id = options.goods_id
      this.getGoodsDetail(id)    
    },
    computed:{
      ...mapState('my_cart',['cart']),
      ...mapGetters('my_cart',['total'])
    },
    watch:{
      // 监听购物车商品数量
      total:{
        handler(newVal){
        // 2. 通过数组的 find() 方法，找到购物车按钮的配置对象
        const findResult = this.options.find((x) => x.text === '购物车')
        if (findResult) {
            // 3. 动态为购物车按钮的 info 属性赋值
             findResult.info = newVal
          }
        },
        immediate:true
      }
    },
    data() {
      return {
        // 商品信息
        goodsInfo:{},
        // 商品导航配置
        options: [{
        			icon: 'shop',
        			text: '店铺',        			        			
        		},
            {
            	icon: 'star',
            	text: '收藏'
            }, 
            {
        			icon: 'cart',
        			text: '购物车',
        			info: 0
        		}],
        	    buttonGroup: [{
        	      text: '加入购物车',
        	      backgroundColor: '#42b983',
        	      color: '#fff'
        	    },
        	    {
        	      text: '立即购买',
        	      backgroundColor: '#1677ff',
        	      color: '#fff'
        	    }
        	    ]
      };
    },
    methods:{
      ...mapMutations('my_cart', ['addToCart']),
      async getGoodsDetail(id){
        const {data:res} = await getShopDetail(id)
        if(res.meta.status !== 200) return uni.$showMsg()
        res.message.goods_introduce = res.message.goods_introduce
              .replace('/<img /g','<img style="display:block;"')
              .replace('/.webp/g','.jpg')
        this.goodsInfo = res.message
      },
      // 预览轮播图图片
      previewImg(i){
        uni.previewImage({
          current:i,
          urls:this.goodsInfo.pics.map(x => x.pics_big)
        })
      },
      // 商品导航 左侧点击事件
      onClick(e){
        if(e.content.text === '购物车'){
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },
      // 商品导航 右侧点击事件
      buttonClick(e){  
        if (e.content.text === '加入购物车') {
              const goods = {
                 goods_id: this.goodsInfo.goods_id,       // 商品的Id
                 goods_name: this.goodsInfo.goods_name,   // 商品的名称
                 goods_price: this.goodsInfo.goods_price, // 商品的价格
                 goods_count: 1,                           // 商品的数量
                 goods_small_logo: this.goodsInfo.goods_small_logo, // 商品的图片
                 goods_state: true                         // 商品的勾选状态
              }
            this.addToCart(goods)
          }
      },
    },
  }
</script>

<style lang="scss">
.goods-swiper{
  height: 300px;
  image{
    height: 100%;
    width: 100%;
  }
}

.goods-introduce{
  padding: 10px;
  background-color: white;
  margin-top: 5px;
  margin-bottom: 10px;
  .price{
    color: red;
    font-size: 20px
  }
  .introduce{
    font-size: 13px
  }
}
.goods-nav{
  position: fixed;
  bottom: 0;
  left:0;
  width: 100%;
}
</style>
